package com.mukefile.dao.impl;

import com.mukefile.dao.UserFolderDao;
import com.mukefile.dto.DeleteFileDto;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/12
 */
@Repository
public class UserFolderDaoImpl implements UserFolderDao {
    @Resource
    HibernateTemplate hibernateTemplate;
    @Override
    public void batchUpdateFolder(List<DeleteFileDto> list,String deleted) {
        if (list.size() == 0){
            return;
        }
        StringBuilder sql = new StringBuilder("update muke_user_folder set deleted=? where id in (");
        for (DeleteFileDto folderDto: list){
            sql.append("?,");
        }
        sql.setCharAt(sql.length()-1,')');
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createSQLQuery(sql.toString()).setParameter(1,deleted);
        int i = 2;
        for (DeleteFileDto folderDto:list){
            query.setParameter(i,folderDto.getId());
            i++;
        }
        query.executeUpdate();
    }
}
